// pages/user_centre/breakThrough/breakThrough.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    words: [],
    finished: false,
		deleteNumber:0
  },

  /**
   * 点击确认删除按钮
   */
  removeWrong() {
    const thiz = this
		const id = this.data.currentWordId
		const index = this.data.currentIndex
    thiz.data.words.splice(this.data.currentIndex, 1)
    thiz.setData({
      words: thiz.data.words,
			deleteNumber:thiz.data.deleteNumber + 1
    })
		this.hideModal()
    if (id!=null && index!=null) {
      wx.cloud.callFunction({
        // 要调用的云函数名称
        name: 'removeWrong',
        // 传递给云函数的参数
        data: {
          word_id: id,
        },
        success: res => {
          console.log("function removeWrong", res)
          if (res.result.msg == 'ok') {
            console.log("删除成功")
          }
        },
        fail: err => {
          // handle error
          console.log("In function removeWrong", err)
        },
        complete: () => {
          // ...
        }
      })
    }

  },

  /**
   * 长按删除
   */
  deleteModal(e) {
    console.log("e", e)
		let currentWordId = e.currentTarget.dataset.id
		let currentIndex = e.currentTarget.dataset.index
    this.setData({
      modalName: "bottomModal",
      currentWordId: currentWordId,
      currentIndex: currentIndex
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null,
      currentWordId: null,
      currentIndex: null
    })
  },

  /**
   * 从base开始加载错题，返回的是第base+1个到x个
   */
  loadWords(base) {
    const thiz = this
    thiz.setData({
      loadModal: true
    })
    wx.cloud.callFunction({
      // 要调用的云函数名称
      name: 'getWrong',
      // 传递给云函数的参数
      data: {
        alreadyHave: base
      },
      success: res => {
        // output: res.result === 3
        console.log("callFunction res", res)
        if (res.result.words.length != 0) {

          for (let i = 0; i < res.result.num.length; i++) {
            res.result.words[i].number = res.result.num[i]
          }
          let newWords = thiz.data.words.concat(res.result.words)

          thiz.setData({
            words: newWords
          })
        } else {
          thiz.setData({
            modalName: "Modal",
            finished: true
          })
        }
      },
      fail: err => {
        // handle error
      },
      complete: () => {
        // ...
        thiz.setData({
          loadModal: false
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.loadWords(0)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.words.length != 0) {
      this.loadWords(this.data.words.length + this.data.deleteNumber)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  hideModal(e) {
    this.setData({
      modalName: null
    })
  }
})